package com.cs4261.smartalarm.component;

import java.util.List;

import com.cs4261.smartalarm.R;
import com.cs4261.smartalarm.backend.Alarm;

import android.content.Context;
import android.content.res.Resources;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

public class AlarmAdapter extends ArrayAdapter<Alarm> {
	
	private List<Alarm> alarmList;
	
	/**
	 * @param context
	 * @param textViewResourceId
	 */
	public AlarmAdapter(Context context, int textViewResourceId) {
	    super(context, textViewResourceId);
	    // TODO Auto-generated constructor stub
	}
	
	
	/**
	 * @param context
	 * @param resource
	 * @param alarmList
	 */
	public AlarmAdapter(Context context, int resource, List<Alarm> alarmList) {
		super(context, resource, alarmList);
		// TODO Auto-generated constructor stub
		
		this.alarmList = alarmList;
	}

	

	/* (non-Javadoc)
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View view = convertView;
		
		if(view == null) {
			LayoutInflater viewI;
			viewI = LayoutInflater.from(getContext());
			view = viewI.inflate(R.layout.alarm_entity, null);
		}
		else {
			view = convertView;
		}
		
		Resources res = view.getResources();
		Alarm alarm = getItem(position);
		ToggleButton toggleAlarm = (ToggleButton)view.findViewById(R.id.button_toggleAlarm);
		TextView id = (TextView)view.findViewById(R.id.alarm_id);
		TextView name = (TextView)view.findViewById(R.id.alarm_name);
		TextView time = (TextView)view.findViewById(R.id.alarm_time);
		TextView repeat = (TextView)view.findViewById(R.id.indicator_repeat);
		TextView weather = (TextView)view.findViewById(R.id.indicator_weather);
		TextView traffic = (TextView)view.findViewById(R.id.indicator_traffic);
		
		
		//repeat...
		//tone...
		
		
		toggleAlarm.setChecked(alarm.getActive() == 1 ? true : false);
		id.setText(alarm.getId() + "");
		name.setText(alarm.getName());
		
		String timeText = (alarm.getHour()%12 == 0 ? 12 : alarm.getHour()%12) + ":" + (alarm.getMinute() < 10 ? "0" + alarm.getMinute() : alarm.getMinute()) + (alarm.getHour() > 12 ? " PM" : " AM");
		time.setText(timeText);
		
		if(alarm.getRepeat() == 1) {
			repeat.setText("On");
			repeat.setTextColor(res.getColor(R.color.dark_blue));
			((View) view.findViewById(R.id.layout_repeatConditions)).setVisibility(View.VISIBLE);
			//Show conditions
			if(alarm.getSunday() == 1) ((TextView)view.findViewById(R.id.indicator_sunday)).setTextColor(res.getColor(R.color.dark_green));
			if(alarm.getMonday() == 1) ((TextView)view.findViewById(R.id.indicator_monday)).setTextColor(res.getColor(R.color.dark_green));
			if(alarm.getTuesday() == 1) ((TextView)view.findViewById(R.id.indicator_tuesday)).setTextColor(res.getColor(R.color.dark_green));
			if(alarm.getWednesday() == 1) ((TextView)view.findViewById(R.id.indicator_wednesday)).setTextColor(res.getColor(R.color.dark_green));
			if(alarm.getThursday() == 1) ((TextView)view.findViewById(R.id.indicator_thursday)).setTextColor(res.getColor(R.color.dark_green));
			if(alarm.getFriday() == 1) ((TextView)view.findViewById(R.id.indicator_friday)).setTextColor(res.getColor(R.color.dark_green));
			if(alarm.getSaturday() == 1) ((TextView)view.findViewById(R.id.indicator_saturday)).setTextColor(res.getColor(R.color.dark_green));
		}
		else {
			((View) view.findViewById(R.id.layout_repeatConditions)).setVisibility(View.GONE);
		}
		
		/**
		if(alarm.getWeatherActive() == 1) {
			weather.setText("On");
			weather.setTextColor(res.getColor(R.color.dark_blue));
			((View) view.findViewById(R.id.layout_weatherConditions)).setVisibility(View.VISIBLE);
			//Show conditions and offsets
			if(alarm.getW1() > -1) {
				TextView text = ((TextView)view.findViewById(R.id.indicator_clear));
				if(alarm.getW1() == 0) {
					text.setText(text.getText().toString() + " " + alarm.getW1());
				}
				else {
					text.setText(text.getText().toString() + " " + alarm.getW1());
					text.setTextColor(res.getColor(R.color.dark_blue));
				}
			}
			
			if(alarm.getW2() > -1) {
				TextView text = ((TextView)view.findViewById(R.id.indicator_fog));
				if(alarm.getW2() == 0) {
					text.setText(text.getText().toString() + " " + alarm.getW2());
				}
				else {
					text.setText(text.getText().toString() + " " + alarm.getW2());
					text.setTextColor(res.getColor(R.color.dark_blue));
				}
			}
			
			if(alarm.getW3() > -1) {
				TextView text = ((TextView)view.findViewById(R.id.indicator_rain));
				if(alarm.getW3() == 0) {
					text.setText(text.getText().toString() + " " + alarm.getW3());
				}
				else {
					text.setText(text.getText().toString() + " " + alarm.getW3());
					text.setTextColor(res.getColor(R.color.dark_blue));
				}
			}
			
			if(alarm.getW4() > -1) {
				TextView text = ((TextView)view.findViewById(R.id.indicator_snow));
				if(alarm.getW4() == 0) {
					text.setText(text.getText().toString() + " " + alarm.getW4());
				}
				else {
					text.setText(text.getText().toString() + " " + alarm.getW4());
					text.setTextColor(res.getColor(R.color.dark_blue));
				}
			}			
		}
		else {
			((View) view.findViewById(R.id.layout_weatherConditions)).setVisibility(View.GONE);
		}**/
		
		if(alarm.getWeatherActive() == 1) {
			weather.setText("On");
			weather.setTextColor(res.getColor(R.color.dark_blue));
			((View) view.findViewById(R.id.layout_weatherConditions)).setVisibility(View.VISIBLE);
			//Show conditions and offsets
			if(alarm.getW1() > -1) {
				ImageView img = ((ImageView)view.findViewById(R.id.indicator_clear));
				if(alarm.getW1() == 0) {
					
				}
				else {
					img.setImageResource(R.drawable.fair);
					img.refreshDrawableState();
				}
			}
			
			if(alarm.getW2() > -1) {
				ImageView img = ((ImageView)view.findViewById(R.id.indicator_fog));
				if(alarm.getW2() == 0) {
					
				}
				else {
					img.setImageResource(R.drawable.fog);
					img.refreshDrawableState();
				}
			}
			
			if(alarm.getW3() > -1) {
				ImageView img = ((ImageView)view.findViewById(R.id.indicator_rain));
				if(alarm.getW3() == 0) {
					
				}
				else {
					img.setImageResource(R.drawable.rain);
					img.refreshDrawableState();
				}
			}
			
			if(alarm.getW4() > -1) {
				ImageView img = ((ImageView)view.findViewById(R.id.indicator_snow));
				if(alarm.getW4() == 0) {
					
				}
				else {
					img.setImageResource(R.drawable.cold);
					img.refreshDrawableState();
				}
			}			
		}
		else {
			((View) view.findViewById(R.id.layout_weatherConditions)).setVisibility(View.GONE);
		}
		
		if(alarm.getTrafficActive() == 1) {
			traffic.setText("On");
			traffic.setTextColor(res.getColor(R.color.dark_blue));
			((View) view.findViewById(R.id.layout_trafficConditions)).setVisibility(View.VISIBLE);
			//Show locations
			
			
		}
		else {
			((View) view.findViewById(R.id.layout_trafficConditions)).setVisibility(View.GONE);
		}		
		
		
		//weather.setText(alarm.getWeatherActive() == 1 ? "On" : "Off");
		//traffic.setText(alarm.getTrafficActive() == 1 ? "On" : "Off");
		
		((View) view.findViewById(R.id.detail_layout)).setVisibility(View.GONE);
		
		return view;
	}
	
	
}